He said that he wasn't including C99 single line comments, only block comments.
&& has higher priority than ||, so if you don't remove it, then you need to put paren around the all the or statements to make it work right.
He said that he wasn't including C99 single line comments, only block comments.
&& has higher priority than ||, so if you don't remove it, then you need to put paren around the all the or statements to make it work right.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Warning Options - Using the GNU Compiler Collection (GCC)
andThis enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros. This also enables some language-specific warnings described in C++ Dialect Options and Objective-C and Objective-C++ Dialect Options.
[edit]Note that some warning flags are not implied by -Wall. Some of them warn about constructions that users generally do not consider questionable, but which occasionally you might wish to check for; others warn about constructions that are necessary or hard to avoid in some cases, and there is no simple way to modify the code to suppress the warning. Some of them are enabled by -Wextra but many of them must be enabled individually.
Using -Wall will often draw my attention to potential bugs.
"-Wall" is specific to GCC, your compiler may have a similar option though. Which compiler is it?I tried writing it into parameters of my compilers, it still doesnt recognizes the case with comments you mentioned.
Last edited by Aksel; 11-22-2009 at 07:32 AM.
Anyways, i shortened the code alot, the only reason i had squote states so much last time was the syntax checker from which i took most of the code. It dealt with the states aswell, just much more detail.
So here it is, the most shortened version:
Code:#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int c; enum states { normal, squote, squote_escape, comment_entry, comment, comment_exit, comment_exit2, comment2, dquote, dquote_escape }; int state = normal; int second; /* stores the tmp character after slash */ int flag=0; /* if 1, puts the character stored in second */ while((c=getchar())!=EOF) { if(state == normal && c=='\'') { state = squote; } else if(state == normal && c=='"') { state = dquote; } else if(state == normal && c=='/') { second=getchar(); if(second=='*') { ungetc(c, stdin); state = comment; } else if(second=='/') { ungetc(c, stdin); state = comment2; } else if(second=='\'') {state = squote; } else if(second=='"') { state = dquote; } else flag=1; } else if(state == comment && c=='*') { state = comment_exit; } else if(state == comment) {} else if(state == comment_exit && c=='*') {} else if(state == comment_exit && c=='/') { putchar(' '); state = comment_exit2; } else if(state == comment_exit) { state = comment; } else if(state == comment2 && c=='\n') { state = normal; } else if(state == comment2) { } else if(state == squote && c=='\\') { state = squote_escape; } else if(state == squote && (c=='\'' || c=='\n')) { state = normal; } else if(state == squote) { } else if(state == squote_escape && c=='\n') { state = normal; } else if(state == squote_escape) { state = squote; } else if(state == dquote && c=='\n') { state = normal; } else if(state == dquote && c=='"') { state = normal; } else if(state == dquote && c=='\\') { state = dquote_escape; } else if(state == dquote) { } else if(state ==dquote_escape) { state = dquote; } if((state == normal || state == squote || state == squote_escape || state == dquote || state == dquote_escape) && state!=comment_exit2) putchar(c); if(state==comment_exit2) { state = normal; } if(flag==1) { flag=0; putchar(second); } } printf("Press any key to continue"); getchar(); return 0; }
Last edited by Tool; 11-22-2009 at 07:39 AM.
insertCode:/* comment line */ //comment line /* comment line */ #include"stdio.h" void main(int argc,char *argv[]) { char ch,ch1,ch2; FILE *fp,*fp1; fp=fopen("filename","r");/*in place of "filename" u can put a command line argument file argv[1]*/ fp1=fopen("temp","w"); if(!fp || !fp1) { printf("cannot open file "); exit(0); } while((ch=getc(fp)) != EOF) { if(ch=='/') if( ((ch1=getc(fp))!= EOF) && (ch1=='*') ) do{ if(ch1=='*' && ((ch2=getc(fp))!= EOF) && ch2=='/') break; continue; }while((ch1=getc(fp))!= EOF); else if(ch1=='/') do{ if( ((ch2=getc(fp)) != EOF) && ch2=='\n') break; continue; }while((ch1=getc(fp))!= EOF); else { putc(ch,fp1); putc(ch1,fp1); } else putc(ch,fp1); } fclose(fp); fclose(fp1); remove("filename"); rename("temp","filename"); }
Last edited by c's mad; 11-29-2009 at 05:01 AM.